import gtk


def doNew(menuItem):
    pass


def doOpen(menuItem):
    pass


def doSave(menuItem):
    pass


def doSaveAs(menuItem):
    pass


def doQuit(*args):
    gtk.main_quit()


def doIncreaseFontSize(menuItem, pangoBuffer):
    bounds = pangoBuffer.buf.get_selection_bounds()
    selectedText = pangoBuffer.buf.get_slice(*bounds)
    # create new tag for selection - XXX note that this is not optimal and that
    # we'd ideally not stack new tags upon old tags, but I couldn't figure out
    # how to do that efficiently
    default = 10 * 1024
    sizeStep = 0.5 * 1024
    tag = pangoBuffer.buf.create_tag()
    # get all defined sizes for the selected text
    sizes = [default]
    for tag in pangoBuffer.get_selection()[0].get_tags():
        if tag:
            size = tag.get_property("size")
            if size != 1:
                sizes.append(size)
    # get the largest size
    startSize = max(sizes)
    # add some increment to the size
    newSize = startSize + sizeStep
    # then set_property("size", value)
    tag.set_property("size", newSize)
    # apply to the selection
    pangoBuffer.buf.apply_tag(tag, *pangoBuffer.get_selection())

def doPrintMarkup(button, pangoBuffer):
    print pangoBuffer.get_text()


def doNormalize(button, pangoBuffer):
    pangoBuffer.remove_all_tags()


def doSetMark(textBuffer, textIterator, textMark, pangoBuffer):
    if textMark != pangoBuffer.insert:
        return
    for tags, widget in pangoBuffer.tag_widgets.items():
        isActive = True
        for tag in tags:
            if not textIterator.has_tag(tag):
               isActive = False
        pangoBuffer.internal_toggle = True
        widget.set_active(isActive)
        pangoBuffer.internal_toggle = False


def doToggleButton(widget, tags, pangoBuffer):
    if pangoBuffer.internal_toggle:
        return
    for tag in tags:
        # apply tag
        if widget.get_active():
            pangoBuffer.apply_tag(tag)
        # remove tag
        else:
            pangoBuffer.remove_tag(tag)


